

var pinyin = require('node-pinyin');
var utils = {

    letter: function (title) {
        title = title.replace(/(^s*)|(s*$)/g, "");
        let str = title.substring(0, 1).toUpperCase();

        // 英文字符，数字
        if (str >= 'A' && str <= 'Z') {
            return str;
        } else if (str >= 0 && str <= 9) {
            return '0-9';
        } else {

            var p = pinyin(str, { "style": "normal" })[0][0].toUpperCase();
            // console.log(p);
            p = p.substring(0, 1).toUpperCase();


            // console.log((p >= 'A') && (p <= 'Z'));

            if ((p >= 'A') && (p <= 'Z')) {
                return p;
            } else {
                return '其他';
            }


        }

    },
    timeFormat: function (date) {

        var _date;

        if (date.indexOf("今天") != -1) {

            _date = this.dateFormat(new Date());

            date = date.replace("今天", _date);
            date += ":00";

        } else if (date.indexOf("昨天") != -1) {

            _date = new Date();

            _date.setDate(_date.getDate() - 1);

            _date = this.dateFormat(_date);


            date = date.replace("昨天", _date);
            date += ":00";


        } else if (date.indexOf("前天") != -1) {

            _date = new Date();

            _date.setDate(_date.getDate() - 2);

            _date = this.dateFormat(_date);

            date = date.replace("前天", _date);
            date += ":00";

        } else if (date.indexOf("月") != -1) {
            date = date.replace('月', '-');
            date = date.replace('号', '');

            _date = new Date().getFullYear();

            date = _date + "-" + date;
        } else {

        }

        _date = null;


        return new Date(date);


    },
    dateFormat(date) {

        var year = date.getFullYear(),
            month = (date.getMonth() + 1) > 10 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1),
            day = date.getDate() > 10 ? date.getDate() : "0" + date.getDate();
        return year + "-" + month + "-" + day;
    },
    trim: function (str) { //删除左右两端的空格
        return str.replace(/(^\s*)|(\s*$)/g, "");
    },
    ltrim: function (str) { //删除左边的空格
        return str.replace(/(^s*)/g, "");
    },
    rtrim: function (str) { //删除右边的空格
        return str.replace(/(s*$)/g, "");
    },

    kkkmatchData: function (html) {
        var DM5_VIEWSIGN = /\bDM5_VIEWSIGN\b="(.*?)";/ig;
        var DM5_VIEWSIGN_DT = /\bDM5_VIEWSIGN_DT\b="(.*?)";/ig;
        var DM5_MID = /\bDM5_MID\b=(.*?);/ig;
        var DM5_CID = /\bDM5_CID\b=(.*?);/ig;
        var DM5_IMAGE_COUNT = /\bDM5_IMAGE_COUNT\b=(.*?);/ig;


        var _sign = DM5_VIEWSIGN.exec(html);
        var _dt = DM5_VIEWSIGN_DT.exec(html);
        var _mid = DM5_MID.exec(html);
        var _cid = DM5_CID.exec(html);
        var totalPage = DM5_IMAGE_COUNT.exec(html);


        return {
            totalPage: Number(totalPage[1]),
            cid: Number(_cid[1]),
            page: 1,
            key: '',
            language: 1,
            gtk: 6,
            _cid: Number(_cid[1]),
            _mid: Number(_mid[1]),
            _dt: _dt[1],
            _sign: _sign[1]
        };
    },
    dmzjmatchData: function (html) {

        console.log(html);

        var ev = /\beval\b\((.*)\)/ig;

        var match_eval = ev.exec(html);

        console.log(match_eval);

        return {
            totalPage: "(" + match_eval[1] + ")",
            page: 1
        };
    },
    mhgmatchData: function (html) {

        var LZString = (function () {
            var f = String.fromCharCode;
            var keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
            var baseReverseDic = {};
            function getBaseValue(alphabet, character) {
                if (!baseReverseDic[alphabet]) { baseReverseDic[alphabet] = {}; for (var i = 0; i < alphabet.length; i++) { baseReverseDic[alphabet][alphabet.charAt(i)] = i } } return baseReverseDic[alphabet][character]
            } var LZString = { decompressFromBase64: function (input) { if (input == null) return ""; if (input == "") return null; return LZString._0(input.length, 32, function (index) { return getBaseValue(keyStrBase64, input.charAt(index)) }) }, _0: function (length, resetValue, getNextValue) { var dictionary = [], next, enlargeIn = 4, dictSize = 4, numBits = 3, entry = "", result = [], i, w, bits, resb, maxpower, power, c, data = { val: getNextValue(0), position: resetValue, index: 1 }; for (i = 0; i < 3; i += 1) { dictionary[i] = i } bits = 0; maxpower = Math.pow(2, 2); power = 1; while (power != maxpower) { resb = data.val & data.position; data.position >>= 1; if (data.position == 0) { data.position = resetValue; data.val = getNextValue(data.index++) } bits |= (resb > 0 ? 1 : 0) * power; power <<= 1 } switch (next = bits) { case 0: bits = 0; maxpower = Math.pow(2, 8); power = 1; while (power != maxpower) { resb = data.val & data.position; data.position >>= 1; if (data.position == 0) { data.position = resetValue; data.val = getNextValue(data.index++) } bits |= (resb > 0 ? 1 : 0) * power; power <<= 1 } c = f(bits); break; case 1: bits = 0; maxpower = Math.pow(2, 16); power = 1; while (power != maxpower) { resb = data.val & data.position; data.position >>= 1; if (data.position == 0) { data.position = resetValue; data.val = getNextValue(data.index++) } bits |= (resb > 0 ? 1 : 0) * power; power <<= 1 } c = f(bits); break; case 2: return "" }dictionary[3] = c; w = c; result.push(c); while (true) { if (data.index > length) { return "" } bits = 0; maxpower = Math.pow(2, numBits); power = 1; while (power != maxpower) { resb = data.val & data.position; data.position >>= 1; if (data.position == 0) { data.position = resetValue; data.val = getNextValue(data.index++) } bits |= (resb > 0 ? 1 : 0) * power; power <<= 1 } switch (c = bits) { case 0: bits = 0; maxpower = Math.pow(2, 8); power = 1; while (power != maxpower) { resb = data.val & data.position; data.position >>= 1; if (data.position == 0) { data.position = resetValue; data.val = getNextValue(data.index++) } bits |= (resb > 0 ? 1 : 0) * power; power <<= 1 } dictionary[dictSize++] = f(bits); c = dictSize - 1; enlargeIn--; break; case 1: bits = 0; maxpower = Math.pow(2, 16); power = 1; while (power != maxpower) { resb = data.val & data.position; data.position >>= 1; if (data.position == 0) { data.position = resetValue; data.val = getNextValue(data.index++) } bits |= (resb > 0 ? 1 : 0) * power; power <<= 1 } dictionary[dictSize++] = f(bits); c = dictSize - 1; enlargeIn--; break; case 2: return result.join('') }if (enlargeIn == 0) { enlargeIn = Math.pow(2, numBits); numBits++ } if (dictionary[c]) { entry = dictionary[c] } else { if (c === dictSize) { entry = w + w.charAt(0) } else { return null } } result.push(entry); dictionary[dictSize++] = w + entry.charAt(0); enlargeIn--; w = entry; if (enlargeIn == 0) { enlargeIn = Math.pow(2, numBits); numBits++ } } } }; return LZString
        })();
        String.prototype.splic = function (f) { return LZString.decompressFromBase64(this).split(f) };


        var mh_info = /window\["\\x65\\x76\\x61\\x6c"\]\((.+)\)/ig;
        var _mh_info = mh_info.exec(html);
        _mh_info = eval(_mh_info);
        return {
            mh_info: _mh_info
        };
    },
    utf8to16: function (str) {
        var out, i, len, c;
        var char2, char3;
        out = "";
        len = str.length;
        i = 0;
        while (i < len) {
            c = str.charCodeAt(i++);
            switch (c >> 4) {
                case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                    out += str.charAt(i - 1);
                    break;
                case 12: case 13:
                    char2 = str.charCodeAt(i++);
                    out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
                    break;
                case 14:
                    char2 = str.charCodeAt(i++);
                    char3 = str.charCodeAt(i++);
                    out += String.fromCharCode(((c & 0x0F) << 12) |
                        ((char2 & 0x3F) << 6) |
                        ((char3 & 0x3F) << 0));
                    break;
            }
        }

        return out;
    },
    host: function (url) {
        return url.match(/^(\w+:\/\/)?([^\/]+)/i)[0];
    }
};


module.exports = utils;